/*
 * DynamicPlace.java
 * 
 * Created on Jun 8, 2007, 8:18:23 PM
 * 
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.infomancers.workflow.dynamic;

import com.infomancers.collections.Predicate;
import com.infomancers.workflow.Place;
import com.infomancers.workflow.PlaceListener;
import com.infomancers.workflow.Token;
import com.infomancers.workflow.Transition;
import com.infomancers.workflow.Workflow;
import java.util.Collections;
import java.util.List;
import java.util.Map;

/**
 *
 * @author aviadbd
 */
public final class DynamicPlace implements Place {
    private Predicate<Place> predicate;
    private Workflow workflow;
    private Place delegatePlace = null;

    private static Map<String, String> emptyProperties = Collections.unmodifiableMap(Collections.<String, String>emptyMap());
    
    public void setPredicate(Predicate<Place> predicate) {
        this.predicate = predicate;
    }
    
    public Predicate<Place> getPredicate() {
        return predicate;
    }
    
    public void addPlaceListener(PlaceListener listener) {
        assert delegatePlace != null : "Delegate place not set yet";
        delegatePlace.addPlaceListener(listener);
    }

    public void removePlaceListener(PlaceListener listener) {
        assert delegatePlace != null : "Delegate place not set yet";
        delegatePlace.removePlaceListener(listener);
    }

    public void addToken(Token token) {
        assert delegatePlace != null : "Delegate place not set yet";
        delegatePlace.addToken(token);
    }

    public void setOutputTransitions(List<Transition> outputTransitions) {
        assert delegatePlace != null : "Delegate place not set yet";
        delegatePlace.setOutputTransitions(outputTransitions);
    }

    public List<Transition> getOutputTransitions() {
        assert delegatePlace != null : "Delegate place not set yet";
        return delegatePlace.getOutputTransitions();
    }

    public Map<String, String> getProperties() {
        assert delegatePlace != null : "Delegate place not set yet";
        return delegatePlace.getProperties();
    }

    public boolean lockToken(Transition transition, Token token) {
        assert delegatePlace != null : "Delegate place not set yet";
        return delegatePlace.lockToken(transition, token);
    }

    public void removeToken(Transition transition, Token token) {
        assert delegatePlace != null : "Delegate place not set yet";
        delegatePlace.removeToken(transition, token);
    }

    public void unlockToken(Transition transition, Token token) {
        assert delegatePlace != null : "Delegate place not set yet";
        
        delegatePlace.unlockToken(transition, token);
    }
    
    @Override
    public String toString() {
        return delegatePlace == null ? "DyanmicPlace with no delegate set" : delegatePlace.toString();
    }
    
    public void setPlace(Place delegatePlace) {
        assert delegatePlace != null : "Can't set a null delegate";
        
        this.delegatePlace = delegatePlace;
    }
}
